﻿using Microsoft.AspNetCore.Http;

namespace Luka.Log;

internal class ReadBodyMiddleware
{
    // 定义一个只读的RequestDelegate类型的变量_next
    private readonly RequestDelegate _next;

    // 构造函数，接收一个RequestDelegate类型的参数next
    public ReadBodyMiddleware(RequestDelegate next)
    {
        // 将参数next赋值给变量_next
        _next = next;
    }

    // 异步方法，接收一个HttpContext类型的参数context
    public async Task Invoke(HttpContext context)
    {
        // 启用请求体的缓冲
        context.Request.EnableBuffering();
        // 将请求体的位置设置为0
        context.Request.Body.Position = 0;
        // 调用_next方法，传入参数context
        await _next(context);
    }
}